﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Web_Oto.Business;
using Web_Oto.Common;

namespace Web_Oto
{
    public partial class Search : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                GetSeoInformations();
                GetProductSearch(); }
        }

        private void GetSeoInformations()
        {
            var seoBus = new SeoBus();
            var seo = seoBus.GetSeoPage("search");
            if (seo != null)
            {
                Page.Title = seo.Title;
                Page.MetaDescription = seo.Description;
                Page.MetaKeywords = seo.Keyword;
            }
        }

        private void GetProductSearch()
        {
            var productBus = new ProductBus();
            var keyword = (Page.RouteData.Values.ContainsKey("keyword")) ? 
                Page.RouteData.GetRequiredString("keyword") : null;
            var currentpage = (Page.RouteData.Values.ContainsKey("currentPage")) ? 
                int.Parse(Page.RouteData.GetRequiredString("currentPage")) : 1;
            var itemPerPage = int.Parse(ConfigurationManager.AppSettings["ItemsInSearch"]);
            var totalRecord = 0;
            var products = productBus.SearchProduct(keyword, currentpage, itemPerPage,out totalRecord);
            var sb = new StringBuilder();
            if(totalRecord > itemPerPage)
                Paging(currentpage, itemPerPage, totalRecord);

            var path = ConfigurationManager.AppSettings["productPath"];
            foreach (var product in products)
            {
                sb.AppendFormat("<li><a href='/san-pham/{0}/{1}/{2}-{3}.html' class='mask_home'><div></div><img src='{4}/{5}' title='{6}' alt='{6}'></a>",
                    CommonHelper.NiceUrl(product.CategoryParentName.ToLower()), CommonHelper.NiceUrl(product.CategoryName.ToLower()),
                    CommonHelper.NiceUrl(product.Name.ToLower()), product.Id, path, product.Image, product.TitleOfImage);
                sb.AppendFormat("<div><h5><a href='/san-pham/{0}/{1}/{2}-{3}.html' class='product-name'>{4}</a></h5></div>", CommonHelper.NiceUrl(product.CategoryParentName.ToLower()), CommonHelper.NiceUrl(product.CategoryName.ToLower()),
                    CommonHelper.NiceUrl(product.Name.ToLower()), product.Id, product.Name);
                sb.Append("</li>");
            }
            ltrProductsList.Text = sb.ToString();
        }

        private void Paging(int currentPage, int itemPerPage, int totalRecord)
        {
            var sb = new StringBuilder();
            int totalPage = (Convert.ToInt32(totalRecord / itemPerPage) == 0) ? 1 :
                Convert.ToInt32(totalRecord / itemPerPage) + 1;
            var currentUrl = Page.Request.Url.AbsolutePath.Split(new char[] { '/' })[1];
            int firstPage = 1;
            int lastPage = 1;
            int maxPage = Convert.ToInt32(ConfigurationManager.AppSettings["MaxPage"]);
            firstPage = (currentPage < maxPage) ? (currentPage / maxPage) * maxPage + 1 : (maxPage / currentPage) * maxPage;
            lastPage = (currentPage / maxPage > 0) ? (currentPage / maxPage) * maxPage + maxPage : maxPage;
            if (lastPage > totalPage)
                lastPage = totalPage;
            sb.AppendFormat(currentPage == 1 ? "<li>Đầu</li>" : "<li><a href='/{0}/1.html'>Đầu</a></li>", currentUrl);
            for (int page = firstPage; page <= lastPage; page++)
            {
                sb.AppendFormat(
                    page == currentPage ? "<li class='currentpage'>{0}</li>" : "<li><a href='/{1}/{0}.html'>{0}</a></li>",
                    page, currentUrl);
            }
            sb.AppendFormat(currentPage == totalPage ? "<li>Cuối</li>" : "<li><a href='/{0}/{1}.html'>Cuối</a></li>",
                            currentUrl, totalPage);
            ltrPaging.Text = sb.ToString();
        }
    }
}